package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Elector;
import com.skyolder.milkbee.service.ElectorService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;
import com.skyolder.milkbee.web.rest.util.PaginationUtil;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Elector.
 */
@RestController
@RequestMapping("/api")
public class ElectorResource {

    private final Logger log = LoggerFactory.getLogger(ElectorResource.class);
        
    @Inject
    private ElectorService electorService;

    /**
     * POST  /electors : Create a new elector.
     *
     * @param elector the elector to create
     * @return the ResponseEntity with status 201 (Created) and with body the new elector, or with status 400 (Bad Request) if the elector has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/electors")
    @Timed
    public ResponseEntity<Elector> createElector(@RequestBody Elector elector) throws URISyntaxException {
        log.debug("REST request to save Elector : {}", elector);
        if (elector.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("elector", "idexists", "A new elector cannot already have an ID")).body(null);
        }
        Elector result = electorService.save(elector);
        return ResponseEntity.created(new URI("/api/electors/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("elector", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /electors : Updates an existing elector.
     *
     * @param elector the elector to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated elector,
     * or with status 400 (Bad Request) if the elector is not valid,
     * or with status 500 (Internal Server Error) if the elector couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/electors")
    @Timed
    public ResponseEntity<Elector> updateElector(@RequestBody Elector elector) throws URISyntaxException {
        log.debug("REST request to update Elector : {}", elector);
        if (elector.getId() == null) {
            return createElector(elector);
        }
        Elector result = electorService.save(elector);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("elector", elector.getId().toString()))
            .body(result);
    }

    /**
     * GET  /electors : get all the electors.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of electors in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/electors")
    @Timed
    public ResponseEntity<List<Elector>> getAllElectors(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Electors");
        Page<Elector> page = electorService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/electors");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /electors/:id : get the "id" elector.
     *
     * @param id the id of the elector to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the elector, or with status 404 (Not Found)
     */
    @GetMapping("/electors/{id}")
    @Timed
    public ResponseEntity<Elector> getElector(@PathVariable Long id) {
        log.debug("REST request to get Elector : {}", id);
        Elector elector = electorService.findOne(id);
        return Optional.ofNullable(elector)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /electors/:id : delete the "id" elector.
     *
     * @param id the id of the elector to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/electors/{id}")
    @Timed
    public ResponseEntity<Void> deleteElector(@PathVariable Long id) {
        log.debug("REST request to delete Elector : {}", id);
        electorService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("elector", id.toString())).build();
    }

}
